package loginbackgroundfetcher;

import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;

public class ImageDisplayer extends JFrame {
	// 所有图片列表
	private List<ImageFile> imageFiles;
	
	private JPanel contentPane;
	private JList imageList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ImageDisplayer frame = new ImageDisplayer();
					// 最大化窗口
					frame.setExtendedState(JFrame.MAXIMIZED_BOTH);  
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public ImageDisplayer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		
		JButton btnSave = new JButton("保存");
		// 使用匿名内部类，处理按钮的单击操作
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		panel.add(btnSave);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		ImageIcon[] images = loadImage();
		imageList = new JList(images);
		imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(imageList);
	}
	
	/**
	 * 载入所有登录背景图像
	 * @return
	 */
	private ImageIcon [] loadImage() {
		// 图像列表
		List<ImageIcon> iamges = new ArrayList<>();
		try {
			// 载入
			imageFiles = ImageTools.loadImageFiles();
			// 转为 ImageIcon 对象
			for (ImageFile f : imageFiles) {
				ImageIcon ii = new ImageIcon(f.data);
				iamges.add(ii);
			}
			return iamges.toArray(new ImageIcon[iamges.size()]);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "读取登录背景图像失败，" + e.getMessage(), "保存失败", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		return new ImageIcon[0];
	}
	
	/**
	 * 保存图像到文件，单击“保存按钮”时调用
	 */
	private void save() {
		if (imageList.isSelectionEmpty()) {
			JOptionPane.showMessageDialog(this, "请先选择图片，再保存。", "保存失败", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 找到选中图片对应的 ImageFile 对象
		int idx = imageList.getSelectedIndex();
		ImageFile f = imageFiles.get(idx);
		// 准备保存文件选择框
		JFileChooser fileChooser = new JFileChooser();
		if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
			// 获取要保存到的文件文件
			File saveFile = fileChooser.getSelectedFile();
			try {
				// 保存
				ImageTools.saveFile(saveFile, f.data);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "保存图像失败， " + e.getMessage(), "保存失败", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
		}
	}

}
